$(function (){
	//change color in comment row
    $("table#show-table tr:nth-child(odd)").addClass("striped");
    	
	$("#bulk").change(function(e){
		if($("#bulk").val() == 'status')
			$("#changeStatus").show();
		else
			$("#changeStatus").hide();
	});
	
	//search
/*	$("input:checkbox[value='weekly']").click(function(){
		if($("input:checkbox[value='weekly']").attr('checked'))
		{	
			$("#chooseDay").show().insertAfter($("input:checkbox[value='weekly']").parent());	
		}
		else
			$("#chooseDay").hide();
	});*/
	
	$("#form_id").submit(function(){
		if($("#frequency").val() == 'daily' || $("#frequency").val() == 'weekly')
			return true;
		else
		{
			var f = $("#startDate").val();
			var t = $("#finishDate").val();
			if (f.length <= 0)
			{
			/*	alert("You must chose startDate ");
				return false;*/
			}
			else
			{
				var a = f.split(/[/.-]/);
				var startDate = parseInt(a[0]) + parseInt(a[1]) * 35 + parseInt(a[2]) * 500;
				if (t.length == 0)
				{
					alert("You chose startDate but did not choose finishDate");
					return false;
				}
				else 
				{
					var b = t.split(/[/.-]/);
					var finishDate = parseInt(b[0]) + parseInt(b[1]) * 35 + parseInt(b[2]) * 500;
					if (startDate > finishDate)
					{
						alert("startDate must be before finishDate");
						return false;
					}
				}
			}
		}
	});	
	
	//choose type search
	$("#frequency").change(function(e){
		if($("#frequency").val() == 'daily' || $("#frequency").val() == 'weekly')
			$("#searchDate").hide();		
		else
			$("#searchDate").show();
		
		//alert("1232);
	});	
	//bulk action
	$("#bulk-event-form").ajaxForm({
		dataType:  'json',
		success: function(json){
			if(json.bulkaction == 'delete')
			{	
				$.each(json.ids, function(i,id){
		            $("#event-row-" + id).css('background-color','red').fadeOut(2000).remove();
		            console.log("Removing event-box-" + id);	
     			});
    		}
    		else if(json.success == '1')
    		{
   			 	if(json.bulkaction == 'status')
       			{
       				if(json.status == 'deleted' || json.status == 'spam'){
       					$.each(json.ids, function(i,id){
  				            $("#event-row-" + id).css('background-color','blue').fadeOut(2000);
  				            $(".eventIdClass:checkbox:checked").click();
		            	});//end each
		        	}
			        else
			        {
			        	$.each(json.ids, function(i,id){
			            	$(".eventIdClass:checkbox:checked").click();
			            	$("#event-status-" + id).html(json.status);
			           	});
			        }
	     		}
       			else
       				alert('fall');
  			}
   			//set list null
   			json.ids = null;
   		}
	});
	
	//delete href
	$("[id^=delete-event-]").click(function(e){
		var del=confirm("Do you want to delete this event?");
   		deleteId = this.id.split('-')[2];
   		if (del){	
   			$.getJSON("/en/event/ajax/delete-event", {'id': deleteId},
            	function(json) {
                	if(json.success)	
                   		$("#event-row-" + deleteId).css('background-color','red').fadeOut(2000);
                   	else
                    	$("#delete-event-" + deleteId).show().html("Could not delete");
                   return false;
   			});
   		}else
   			return false;
	});
});